package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.domain.cargo.Types;
import cn.itcast.service.FactoryService;
import cn.itcast.utils.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/cargo/factory")
public class FactoryController extends BaseController {
    @Reference
    private FactoryService factoryService;

    @RequestMapping(name = "pageHelper插件分页查询厂家",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        FactoryExample factoryExample = new FactoryExample();
        //设置查询顺序
        factoryExample.setOrderByClause("create_time desc");

        FactoryExample.Criteria criteria = factoryExample.createCriteria();

        // 细颗粒度的数据控制
        if(user.getDegree()==4){
            criteria.andCreateByEqualTo(user.getId()); //根据登录用户
        }else if(user.getDegree()==3){
            criteria.andCreateDeptEqualTo(user.getDeptId()); //根据登录用户的部门
        }

        PageInfo pb = factoryService.findbypage(factoryExample,pageNum, pageSize);
        request.setAttribute("pb",pb);
        return "cargo/factory/factory-list";
    }

    @RequestMapping(name = "新增厂家回显",value = "/toAdd")
    public String toAdd(){

        List<Types> list = new ArrayList<>();
        Types types1 = new Types();
        types1.setCtype("货物");
        Types types2 = new Types();
        types2.setCtype("附件");
        list.add(types1);
        list.add(types2);
        request.setAttribute("types",list);
        return "cargo/factory/factory-add";
    }

    @RequestMapping(name = "修改页面回显",value = "/toUpdate")
    public String toUpdate(String id){

        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);

        List<Types> list = new ArrayList<>();
        Types types1 = new Types();
        types1.setCtype("货物");
        Types types2 = new Types();
        types2.setCtype("附件");
        list.add(types1);
        list.add(types2);
        request.setAttribute("types",list);
        return "cargo/factory/factory-update";
    }

    @RequestMapping(name = "新增/修改合同页面",value = "/edit")
    public String edit(Factory factory){

        if(UtilFuns.isEmpty(factory.getId())){
            factoryService.save(factory);
        }else {
            //修改
            factoryService.update(factory);
        }
        return "redirect:/cargo/factory/list.do";
    }

    @RequestMapping(name = "启用",value = "/submit")
    public String submit(String id){

        Factory factory = factoryService.findById(id);
        if(factory.getState()==0){
            factory.setState(1);
        }
        factoryService.update(factory);
        return "redirect:/cargo/factory/list.do";
    }

    @RequestMapping(name = "新增/修改合同页面",value = "/cancel")
    public String cancel(String id){

        Factory factory = factoryService.findById(id);
        if(factory.getState()==1){
            factory.setState(0);
        }
        factoryService.update(factory);
        return "redirect:/cargo/factory/list.do";
    }

    @RequestMapping(name = "厂家的删除",value = "/delete")
    public String delete(String id){

        factoryService.delete(id);

        return "redirect:/cargo/factory/list.do";
    }

    @RequestMapping(name = "合同的查看",value = "/toView")
    public String toView(String id){

        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);

        return "cargo/factory/factory-view";
    }


}
